package com.jishaokang.service.impl;

import com.jishaokang.base.Result;
import com.jishaokang.cache.ResultCache;
import com.jishaokang.dao.CourseDAO;
import com.jishaokang.dao.LearnDAO;
import com.jishaokang.model.dto.Course;
import com.jishaokang.model.dto.Learn;
import com.jishaokang.model.vo.CourseVO;
import com.jishaokang.model.vo.LearnVO;
import com.jishaokang.service.LearnService;
import com.jishaokang.util.MessageUtil;
import com.jishaokang.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by NANA_Final on 2019/6/21.
 */
@Service
public class LearnServiceImpl implements LearnService {

    @Autowired
    private LearnDAO learnDAO;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private CourseDAO courseDAO;
    @Autowired
    private MessageUtil messageUtil;

    @Override
    public Result selectLearnByCourseIdAndUserId(Learn learn) {
        Integer userId = tokenUtil.getUserIdByToken(learn.getToken());
        learn.setUserId(userId);
        LearnVO learnVO = learnDAO.selectLearnByCourseIdAndUserId(learn);
        if (learnVO == null) return ResultCache.getDataError(501,"请先购买课程");
        return ResultCache.getDataOk(learnVO);
    }

    @Override
    public Result addLearn(Learn learn) {
        Integer userId = tokenUtil.getUserIdByToken(learn.getToken());
        learn.setUserId(userId);
        Learn learnTemp = new Learn();
        learnTemp.setUserId(userId);
        learnTemp.setCourseId(learn.getCourseId());
        LearnVO learnInDB = learnDAO.selectLearnByCourseIdAndUserId(learnTemp);
        if (learnInDB!=null) return ResultCache.getDataError(501,"您已学过该课程");
        learnDAO.insert(learn);
        courseDAO.incLearnTotal(learn.getCourseId());
        Course courseInDB = new Course();
        courseInDB.setCourseId(learn.getCourseId());
        CourseVO courseVO = courseDAO.selectByCourseId(courseInDB);
        messageUtil.sendSystemMessage("成功购买课程 "+courseVO.getCourseName(),userId);
        return ResultCache.OK;
    }

    @Override
    public Result selectLearnByUserId(Learn learn) {
        Integer userId = tokenUtil.getUserIdByToken(learn.getToken());
        learn.setUserId(userId);
        learn.setPage((learn.getPage()-1)*learn.getNumber());
        int total = learnDAO.selectTotalByUserId(learn);
        List<LearnVO> learnVOs = learnDAO.selectByUserId(learn);
        return ResultCache.getListDataOk(total,"courses",learnVOs);
    }

    @Override
    public Result selectCourseComment(Learn learn) {
        System.out.println(learn);
        learn.setPage((learn.getPage()-1)*learn.getNumber());
        int total = learnDAO.selectTotalComment(learn);
        List<LearnVO> learnVOs = learnDAO.selectComment(learn);
        return ResultCache.getListDataOk(total,"comments",learnVOs);
    }

    @Override
    public Result updateCourseComment(Learn learn) {
        int userId = tokenUtil.getUserIdByToken(learn.getToken());
        learn.setUserId(userId);
        learnDAO.updateCourseComment(learn);
        int teacherUserId = courseDAO.selectUserIdByCourseId(learn.getCourseId());
        messageUtil.sendCourseCommentMessage("您有新评论："+learn.getCourseComment(), userId,teacherUserId,learn.getCourseId(),0);
        return ResultCache.OK;
    }

    @Override
    public Result deleteCourseComment(Learn learn) {
        int userId = tokenUtil.getUserIdByToken(learn.getToken());
        learn.setUserId(userId);
        learn.setCourseStar(-1);
        learn.setCourseComment("");
        learnDAO.updateCourseComment(learn);
        return ResultCache.OK;
    }

    @Override
    public Result updateLearnHistory(Learn learn) {
        int userId = tokenUtil.getUserIdByToken(learn.getToken());
        learn.setUserId(userId);
        learnDAO.updateHistory(learn);
        return ResultCache.OK;
    }

    @Override
    public Result selectLearn(Learn learn) {
        learn.setPage((learn.getPage()-1)*learn.getNumber());
        int total = learnDAO.selectTotal(learn);
        List<LearnVO> learnVOs = learnDAO.select(learn);
        return ResultCache.getListDataOk(total,"courses",learnVOs);
    }

    @Override
    public Result selectLearnByCostDesc(Learn learn) {
        learn.setPage((learn.getPage()-1)*learn.getNumber());
        int total = learnDAO.selectTotal(learn);
        List<LearnVO> learnVOs = learnDAO.selectByCostDesc(learn);
        return ResultCache.getListDataOk(total,"courses",learnVOs);
    }

    @Override
    public Result selectLearnByUsername(Learn learn) {
        learn.setPage((learn.getPage() - 1) * learn.getNumber());
        int total = learnDAO.selectTotalByUsername(learn);
        List<LearnVO> learnVOs = learnDAO.selectByUsername(learn);
        return ResultCache.getListDataOk(total, "courses", learnVOs);
    }

}